package com.example.zzl.viewmodel.HomeFragment;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.example.zzl.adapter.HomeFragment.InfoListAdapter;
import com.example.zzl.custom.DividerItemDecoration;
import com.example.zzl.databinding.ActivityInfoListBinding;
import com.example.zzl.model.HomeFragment.InfoListModel;
import com.example.zzl.model.bean.Home;
import com.example.zzl.viewmodel.BaseViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ligg on 2017/2/11.
 */

public class InfoListPresenter implements BaseViewModel, View.OnClickListener {

    private final InfoListModel model;
    private Context context;
    private InfoListCallback callback;
    private ActivityInfoListBinding db;
    private List<Home.News> list;
    private InfoListAdapter adapter;

    public InfoListPresenter(Context context, InfoListCallback callback, ActivityInfoListBinding db) {
        this.context = context;
        this.callback = callback;
        this.db = db;
        model = new InfoListModel(context, this);
    }

    @Override
    public void onClick(View v) {
        callback.onClick(v);
    }

    public interface InfoListCallback {
        String getHeadTitle();
        void onClick(View v);
    }

    @Override
    public void init() {
        list = new ArrayList<Home.News>();
        adapter = new InfoListAdapter(context, list);
    }

    @Override
    public void initView() {
        db.include.toolbar.setNavigationOnClickListener(this);
        db.setHeadTitle(callback.getHeadTitle());
        db.rv.setLayoutManager(new LinearLayoutManager(context));
        db.rv.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL_LIST));
        db.rv.setAdapter(adapter);
    }

    @Override
    public void loadFinished() {
        list.clear();
        list.addAll(model.getList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void loadData(String parameter) {
        model.loadData(parameter);
    }
}
